﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extensil.Common;
//using Extensil.Processor.General;
using Extensil.Category;
using Extensil.Processor.Extensibility;

namespace Extensil
{
    public partial class RightPane :UserForm, ISuperForm
    {
        UserForm _userForm;

        public RightPane(string category)
            : this(CategoryFactory.GetCategoryForm(category))
        {
        }
        public RightPane(UserForm form)
        {
            InitializeComponent();
            _userForm = form;
            LoadForm();
        }
        public UserForm UserForm
        {
            get
            {
                return _userForm;
            }
        }
        public void Reload()
        {
            LoadForm();
        }
        public void ReportStatus(int progress, string message)
        {
        }
        void LoadForm()
        {
            LoadMainForm(_userForm);
            if(PluginAssembly.HasAncestorBaseType(_userForm.GetType(), typeof(BaseCategory)))
                LoadToolsForm((BaseCategory)_userForm);
        }
        void LoadMainForm(UserForm uForm)
        {
            uForm.TopLevel = false;
            uForm.Dock = DockStyle.Fill;
            uForm.FormBorderStyle = FormBorderStyle.None;
            uForm.ContainerForm = (UserForm)this;
            uForm.Show();
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(uForm);
        }
        void LoadToolsForm(BaseCategory catForm)
        {
            lvTools.Items.Clear();
            lvTools.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(lvTools_ItemSelectionChanged);
            ListViewItem lvi;

            if(catForm.ToolNames.Count > 0)
            {
                lvi = new ListViewItem("Home");
                lvTools.Items.Insert(0, lvi);
                splitContainer1.Panel1Collapsed = false;
            }
            else
                splitContainer1.Panel1Collapsed = true;

            foreach(string toolName in catForm.ToolNames)
            {
                lvi = new ListViewItem(toolName);
                lvTools.Items.Add(lvi);
            }
        }
        void lvTools_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if(e.Item.Text == "Home")
                LoadMainForm(_userForm);
            else
            {
                if(PluginAssembly.HasAncestorBaseType(_userForm.GetType(), typeof(BaseCategory)))
                    LoadMainForm(((BaseCategory)_userForm).GetTool(e.Item.Text));
            }
        }
    }
}
